using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace GearBlog.Web.Services
{
	using System.IO;
	using System.Runtime.Remoting.Contexts;
	using Castle.MonoRail.Framework;
	using GearBlog.Web.ConfigSections;

	public class DefaultFileStorageService:IFileStorageService
	{
		public string[] GetFiles(string storageKey, IRailsEngineContext context)
		{
			Storage storage = GetStorage(storageKey);
			string path = Path.Combine(context.ApplicationPhysicalPath, storage.Path);
			return Directory.GetFiles(path);
		}

		public string GetPath(string storageKey, IRailsEngineContext context, string fileTitle)
		{
			Storage storage = GetStorage(storageKey);
			string path = Path.Combine(Path.Combine(context.ApplicationPhysicalPath, storage.Path), fileTitle);
			return path;
		}

		public string GetUrl(string storageKey, IRailsEngineContext context, string fileTitle)
		{
			Storage storage = GetStorage(storageKey);
			string path = Path.Combine(Path.Combine(context.ApplicationPath, storage.Path), fileTitle);
			return path;
		}

		private static Storage GetStorage(string storageKey)
		{
			Storage storage=FileUploadComponentSection.GetSection().Storages[storageKey];
			if (storage == null) throw new ArgumentException("Storage not found.", "storageKey");
			
			return storage;
		}
		public void StoreFile(string fileTitle, Stream inputStream)
		{
			throw new NotImplementedException();
		}
	}
}
